﻿using RemoteServer;
using RRQMMVVM;
using System.Windows.Controls;
using TransferModels;

namespace ViewModel.ViewModels
{
    public class LaunchWindowViewModel : ViewModelBase
    {
        public LaunchWindowViewModel()
        {
            //this.LoginSuccess = action;
            this.LoginOrRegistCommand = new ExecuteCommand<PasswordBox>(LoginOrRegist);
            this.RegisterPanelCommand = new ExecuteCommand(RegisterPanel);
        }

        #region Command

        /// <summary>
        /// 注册或登录按钮
        /// </summary>
        public ExecuteCommand<PasswordBox> LoginOrRegistCommand { get; set; }

        /// <summary>
        /// 调出注册面板
        /// </summary>
        public ExecuteCommand RegisterPanelCommand { get; set; }

        #endregion Command

        private string account;

        public string Account
        {
            get { return account; }
            set { account = value; OnPropertyChanged(); }
        }

        private string registerMessage;

        public string RegisterMessage
        {
            get { return registerMessage; }
            set { registerMessage = value; OnPropertyChanged(); }
        }

        private bool isLaunch = true;

        public bool IsLaunch
        {
            get { return isLaunch; }
            set { isLaunch = value; OnPropertyChanged(); }
        }

        public void RegisterPanel()
        {
            this.IsLaunch = !this.IsLaunch;
            RegisterMessage = string.Empty;
            // LaunchPanelVisible = Visibility.Hidden;
        }

        private void LoginOrRegist(PasswordBox passwordBox)
        {
            if (IsLaunch)
            {
                Login(passwordBox.Password);
            }
            else
            {
                Register(passwordBox.Password);
            }
        }

        private async void Register(string password)//注册
        {
            RegisterMessage = string.Empty;
            if (this.account == null || this.account.Length == 0)
            {
                this.RegisterMessage = "账号不能为空";
                return;
            }
            RegistAccountResult result = await Server.RegistAccount(this.account, password);
            if (result.Status)
            {
                this.RegisterMessage = "注册成功";
            }
            else
            {
                this.RegisterMessage = result.Message;
            }
        }

        private async void Login(string password)
        {
            RegisterMessage = string.Empty;
            if (this.account == null || this.account.Length == 0)
            {
                this.RegisterMessage = "账号不能为空";
                return;
            }
            ConfirmLoginResult result = await Server.ConfirmLogin(this.account, password);
            if (result.Status)
            {
                Messenger.Default.Send("OpenMainWindow");
            }
            else
            {
                this.RegisterMessage = result.Message;
            }
        }
    }
}